package org.weasel.wizards;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class UserStoryWizardPage1 extends WizardPage{
	private Text userstoryNameText;
	@SuppressWarnings("unused")
	private ISelection selection;
	private Text description;
	private Button check;

	public UserStoryWizardPage1(ISelection selection) {
		super("wizardPage");
		setTitle("Userstory Wizard");
        setDescription("Create a new userstory for this project.");
        this.selection = selection;
        setPageComplete(false);
	}
	
	public void createControl(Composite parent) {
		{
			parent.setSize(525, 223);
		}
		// TODO Auto-generated method stub
		Composite container = new Composite(parent, SWT.NULL);
        GridLayout layout = new GridLayout();
        container.setLayout(layout);
        layout.numColumns = 2;
        layout.verticalSpacing = 9;

        Label label = new Label(container, SWT.NULL);
        label.setText("Userstory name:");

        userstoryNameText = new Text(container, SWT.BORDER | SWT.SINGLE);
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 2;
        userstoryNameText.setLayoutData(gd);
        userstoryNameText.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                    dialogChanged();
                }
            });
        
        createLine(container, layout.numColumns);
        
        label = new Label(container, SWT.NULL);
        label.setText("Description:");
        description = new Text(container, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER);
        gd = new GridData(GridData.FILL_BOTH);
        gd.horizontalSpan = 2;
        description.setLayoutData(gd);
        description.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                dialogChanged();
            }
        });
        
        check = new Button(container, SWT.CHECK);
        check.setText("spike card (used to perform experiments)");
        
        setControl(container);
    }

    private void dialogChanged() {
    	String errormsg = null;
        if (getUserstoryName().length() == 0) {
            errormsg = ("Name of the userstory must be specified.\n");
            if (getUserstoryDescription().length() == 0)
            	errormsg+=("Please insert a description for the userstory.");
         }
        if (getUserstoryDescription().length() == 0 && errormsg == null){
        	errormsg=("Please insert a description for the userstory.");
        }
        updateStatus(errormsg);
    }

    private void updateStatus(String message) {
        setErrorMessage(message);
        setPageComplete(message == null);
    }
    
    private void createLine(Composite parent, int ncol) {
        Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL |
                SWT.BOLD);
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.horizontalSpan = ncol;
        gridData.verticalIndent = 20;
        line.setLayoutData(gridData);
    }
    
    public String getUserstoryName() {
    	return userstoryNameText.getText();
    }
    
    public String getUserstoryDescription(){
    	return description.getText();
    }
    
    public boolean isExperimentCard(){
    	return check.getSelection();
    }
}
